package com.gj.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gj.feign.TransactionMessageFeign;
import com.gj.mapper.HouseMapper;
import com.gj.model.House;
import com.gj.model.TransactionMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * author:guijie
 * date:${date}
 * description:TODO
 */
@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private TransactionMessageFeign feign;

    @Override
    public void insertHouse(House house) {
        houseMapper.insert(house);
    }

    @Override
    @Transactional
    public boolean upd(Long houseId, String houseName) {
        House house = new House();
        house.setHouseid(houseId);
        house.setHousename(houseName);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("houseId",houseId);
        int i = houseMapper.update(house,wrapper);
        if (i!=0){    //修改后发消息给消息持久化系统
            TransactionMessage message = new TransactionMessage();
            message.setQueue("transaction");
            message.setcDate(new Date());
            message.setSendSystem("transaction-house");
            message.setMessage(houseId+""+","+houseName);
            boolean b = feign.sendMessage(message);
            if (!b){
                throw new RuntimeException("-------------------回滚事务-----------------");
            }
            return true;
        }
        return false;
    }
}
